package com.dong.dubbo.consumer.service;

import com.dong.dubbo.provider.api.Hello;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.cluster.specifyaddress.Address;
import org.apache.dubbo.rpc.cluster.specifyaddress.UserSpecifiedAddressUtil;
import org.springframework.stereotype.Service;

/**
 * @author dengxiudong
 * @Date 2024/10/14 上午10:10
 */
@Slf4j
@Service
@DubboService
public class TestRemoteCallImpl implements TestRemoteCall
{
    @DubboReference
    private Hello hello;

    @Override
    public void test()
    {
        System.out.println("来此远方的消息:" + hello.sayHello("--------world"));
    }

    @Override
    public void testIdCall()
    {
        ReferenceConfig<Hello> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(Hello.class);
        referenceConfig.setUrl("dubbo://192.168.1.63:20880/com.dong.dubbo.provider.api.Hello");
        Hello hello1 = referenceConfig.get();
        log.info("hello1:{}", hello1);
        if (hello1 == null) {
            throw new IllegalStateException("Failed to get remote service.");
        }
        // UserSpecifiedAddressUtil.setAddress(new Address("192.168.0.196", 0, true));
        log.info("来此远方的消息61:{}", hello1.sayHello("--------world"));

        // UserSpecifiedAddressUtil.setAddress(new Address("192.168.0.181", 0, true));
        log.info("来此远方的消息181:{}", hello1.sayHello("--------world"));
    }

}
